package org.winexpress

import org.codehaus.groovy.grails.web.json.JSONObject;
import org.codehaus.groovy.grails.web.json.JSONArray;
import grails.converters.JSON

/**
 *
 * @author Swee Zhi
 */
class CartItemManagement {
    
    def listCartItems() {
        def cartItems = CartItem.list()
        JSONArray arr = new JSONArray()
        cartItems.each() {
            JSONObject obj = new JSONObject()
            obj.put("id",it.id)
            obj.put("productId",it.product.id)
            obj.put("quantity",it.quantity)
            obj.put("sellingPrice",it.product.sellingPrice)
            obj.put("shoppingCartId",it.shoppingCartId)
            arr.add(obj)
        }
        
        return arr
    }
    
    def getCartItemByID(id) {
        def ci = CartItem.findById(id)
        JSONObject obj = new JSONObject()
        obj.put("id",ci.id)
        obj.put("productId",ci.product.id)
        obj.put("quantity",ci.quantity)
        obj.put("sellingPrice",ci.product.sellingPrice)
        obj.put("shoppingCartId",ci.shoppingCartId)
        return obj
    }
    
    def getCartItemByProductID(productId) {
        def ci = CartItem.findByProductId(productId)
        JSONObject obj = new JSONObject()
        obj.put("id",ci.id)
        obj.put("productId",ci.product.id)
        obj.put("quantity",ci.quantity)
        obj.put("sellingPrice",ci.product.sellingPrice)
        obj.put("shoppingCartId",ci.shoppingCartId)
        return obj
    }
    
    def getCartItemForDbByProductID(inventoryName) {
        def ci = CartItem.findByProductId(inventoryName)
        
        return ci
    }
    
    def addCartItem(o) {
        def status
        
        CartItem ci = new CartItem()
        println 'o.get("productId")' + o.get("productId")
        println 'o.get("quantity")' + o.get("quantity")
        println 'o.get("cartId")' + o.get("cartId")
        
        ProductManagement pman = new ProductManagement()
        ShoppingCartManagement scman = new ShoppingCartManagement()
        
        ci.product = pman.getProductForDbByProductID(o.get("productId"))
        println ci.product
        ci.shoppingCart = scman.getShoppingCartForDbByCartID(o.get("cartId"))
        println ci.shoppingCart
        ci.quantity = o.get("quantity").toInteger()
        
        
        if (ci.save(flush:true)) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Add CartItem >> ' + status
        
        return status
    }
    
    def updateCartItem(cart) {
        def status
        
        CartItem ci = CartItem.findById(cart.id)
        println ci
//        if (cart.has("productId")) { ci.productId = cart.product.id }
        if (cart.has("quantity")) { ci.quantity = cart.get("quantity").toInteger() }
        println ci.quantity
        println cart.get("quantity").toInteger()

        if (ci.save(flush:true)) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Update CartItem >> ' + status
        
        return status
    }
    
    def updateQuantity(cartId, qty) {
        def status
        
        CartItem ci = CartItem.findById(cartId)
        def updatedQty = ci.quantity.toInteger() + qty.toInteger()
        println 'updated quantity: ' + updatedQty
        println 'CartItem in updateQuan :: ' + ci
//        if (cart.has("productId")) { ci.productId = cart.product.id }
        ci.quantity = updatedQty

        if (ci.save(flush:true)) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Update CartItem >> ' + status
        
        return status
    }
    
    def deleteCartItem(id) {
        def status
        
        CartItem ci = CartItem.findById(id)
        if (ci.delete()) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Delete CartItem >> ' + status
        
        return status
    }
}

